package com.turnengine.client.local.group.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.group.bean.GroupSerializer;
import com.turnengine.client.local.group.bean.IGroup;
import java.io.IOException;

/**
 * The Get Group By Id Data Serializer.
 */
public class GetGroupByIdDataSerializer extends AbstractCommandDataSerializer<IGroup> implements IGetGroupByIdDataSerializer {

	/**
	 * Creates a new Get Group By Id Data Serializer.
	 */
	public GetGroupByIdDataSerializer() {
		super(GetGroupById.COMMAND_ID);
	}

	@Override
	public IGroup readReturnValue(IDataReader reader) throws IOException {
		IGroup value = new GroupSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGroup value) throws IOException {
		new GroupSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGroup> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int id = reader.readInt();

		// Create object
		GetGroupById object = new GetGroupById();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setId(id);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGroup> argument) throws IOException {
		GetGroupById object = (GetGroupById) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int id = object.getId();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(id);
	}

	@Override
	public IExecutableCommandResponse<IGroup> newResponse() {
		return new GetGroupByIdResponse();
	}
}
